import { BufferGeometry, Float32BufferAttribute} from "three";

/** DEM几何数据 */
export default class DEMBufferGeometry extends BufferGeometry {    
    public type: any;
    public parameters: any;    

    constructor(demValue:any,demHeightAdjust:any,demWidth: number = 1, demHeight: number = 1,width: number = 1, height: number = 1, widthSegments: number = 1, heightSegments: number = 1) {
        super();

        this.type = 'DEMBufferGeometry';
        this.parameters = {
            width: width,
            height: height,
            widthSegments: widthSegments,
            heightSegments: heightSegments
        };
        let width_half = width / 2;
        let height_half = height / 2;
        let gridX = Math.floor(widthSegments) || 1;
        let gridY = Math.floor(heightSegments) || 1;
        let gridX1 = gridX + 1;
        let gridY1 = gridY + 1;
        let segment_width = width / gridX;
        let segment_height = height / gridY;
        let ix:number, iy:number;
        let indices = [];
        let vertices = [];
        let normals = [];
        let uvs = [];
        let demXStep;
        let demYStep;
        let MinDEMValue = -11000;
        if(demValue != null)
        {
            demXStep = Math.floor(demWidth / gridX1);
            demYStep = Math.floor(demHeight / gridY1);
        }

        let demPixel:any;   
        let yPosAdjust;
        let xPosAdjust;   
        for (iy = 0; iy < gridY1; iy++) {
            let y = iy * segment_height - height_half;
            if(iy === gridY)
            {
                yPosAdjust = demHeight-1;
            }
            else
            {
                yPosAdjust = iy*demYStep;
            }
            for (ix = 0; ix < gridX1; ix++) {
                let x = ix * segment_width - width_half;
                if(demValue != null)
                { 
                    if(ix === gridX)
                    {
                        xPosAdjust = demWidth - 1;
                    }
                    else
                    {
                        xPosAdjust = ix*demXStep;
                    }

                    demPixel = demValue[(yPosAdjust)*demWidth + (xPosAdjust)] - demHeightAdjust;

                    if(demPixel < MinDEMValue)
                    {
                        demPixel = 0;
                    }
                    vertices.push(x, - y, demPixel);

                }
                else
                {
                    vertices.push(x, - y, 0); //demHeightAdjust
                }
                normals.push(0, 0, 1);
                uvs.push(ix / gridX);
                uvs.push(1 - (iy / gridY));
            }
        }
        for (iy = 0; iy < gridY; iy++) {
            for (ix = 0; ix < gridX; ix++) {
                let a = ix + gridX1 * iy;
                let b = ix + gridX1 * (iy + 1);
                let c = (ix + 1) + gridX1 * (iy + 1);
                let d = (ix + 1) + gridX1 * iy;
                indices.push(a, b, d);
                indices.push(b, c, d);
            }
        }
        this.setIndex(indices);
        this.setAttribute('position', new Float32BufferAttribute(vertices, 3));
        this.setAttribute('normal', new Float32BufferAttribute(normals, 3));
        this.setAttribute('uv', new Float32BufferAttribute(uvs, 2));
    }       
}
